#!/usr/bin/env python

import zmq

ctx = zmq.Context()

class Client(object):
	def __init__(self,socktype,port):
		assert isinstance(port,str),"port must be a string type."
		addr = "tcp://localhost:{0}".format(port)
		self.sock_ = ctx.socket(socktype)
		print("Client connect to {0}.".format(addr))
		self.sock_.connect(addr)

	def setSocketSub(self):
		self.sock_.setsockopt(zmq.SUBSCRIBE,'')

	def send(self, msg):
		self.sock_.send(msg)

	def recv(self):
		return self.sock_.recv()

class ClientSub(Client):
	def  __init__(self,port):
		super(ClientSub,self).__init__(socktype=zmq.SUB,port=port)
		super(ClientSub,self).setSocketSub()

class ClientReq(Client):
	def __init__(self,port):
		super(ClientReq,self).__init__(socktype=zmq.REQ,port=port)

if __name__=="__main__":
	try:
		clientsub = ClientSub(port="6990")
		clientreq = ClientReq(port="7970")
		# send message
		print("client recv pub messge...")
		msg = clientsub.recv()
		print("recv > {0}".format(msg))
		print("client send message....")
		clientreq.send("aaa")
		ret = clientreq.recv()
		print("Recv: {0}".format(ret))

		print("client exit.")
	except Exception as e:
		print("Error: {0}".format(str(e)))